//
//  newsDetailViewController.m
//  timemovie
//
//  Created by Yiqiao on 15/7/22.
//  Copyright (c) 2015年 Yiqiao. All rights reserved.
//

#import "newsDetailViewController.h"
#import "common.h"
#import "imageModel.h"
#import "imageCollectionViewCell.h"
#import "photoViewController.h"
#import "baseNavController.h"
@interface newsDetailViewController ()

@end

@implementation newsDetailViewController
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    photoViewController *vc = [[photoViewController alloc]init];
    NSMutableArray *imageUrlArray = [[NSMutableArray alloc]init];
    for (int i=0; i<_imageArray.count; i++) {
        imageModel *model = _imageArray[i];
        NSString *imageUrl = model.imageName;
        [imageUrlArray addObject:imageUrl];
        vc.currentTag = indexPath.row;
    }
    vc.imageUrlArray = imageUrlArray;
    baseNavController *nav = [[baseNavController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)_createNewsDetail{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((w-50)/4,(w-50)/4 );
    _detailCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _detailCollectionView.delegate = self;
    _detailCollectionView.dataSource = self;
    [_detailCollectionView registerClass:[imageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_detailCollectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _imageArray.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    imageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    imageModel *model =[[imageModel alloc]init];
    model = _imageArray[indexPath.row];
    cell.model = model;
    return cell;
}


- (void)loadData{

    _imageArray = [[NSMutableArray alloc]init];
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"image_list.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *subject=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    
    for (NSDictionary *dictionary in subject) {
        imageModel *model = [[imageModel alloc]init];
        model.imageName = [dictionary objectForKey:@"image"];
        [_imageArray addObject:model];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createNewsDetail];
    [self loadData];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
